//
//  EARecommendMealController.m
//  WhatToEat
//
//  Created by 方力 on 15/10/31.
//  Copyright © 2015年 Mac. All rights reserved.
//

#import "EARecommendMealController.h"
#import "EARecommendMealCell.h"


@interface EARecommendMealController ()
@property(strong,nonatomic)NSMutableArray * foods;

@property(strong,nonatomic)UIButton * addDiningtable;

@property(strong,nonatomic)UIButton * repairBtn;
@end

@implementation EARecommendMealController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationItem.title=@"推荐选餐";
 

    
    
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"EARecommendMealCell" bundle:nil] forCellReuseIdentifier:@"EARecommendMealCell"];
    self.tableView.separatorStyle=UITableViewCellSelectionStyleNone;
    
    [self updateData];
    
    
}


#pragma mark------请求
-(void)updateData
{
    
    NSLog(@"%@",ApplicationDelegate.userDictionary);

    NSMutableDictionary * mutableDict=[@{
                                        @"mealtype":self.mealtype,
                                        @"aty_level":self.aty_level
                                        
                                        } mutableCopy];
    
    

    
    [mutableDict setValue:ApplicationDelegate.userDictionary[@"weight"] forKey:@"birthday"];
    [mutableDict setValue:ApplicationDelegate.userDictionary[@"height"] forKey:@"height"];
    [mutableDict setObject:ApplicationDelegate.userDictionary[@"sex"] forKey:@"sex"];
    [mutableDict setValue:ApplicationDelegate.userDictionary[@"birthday"] forKey:@"birthday"];

    
    
    [WebAPI webAPISignAddMd5tWithNote:@"user/recommendMeal2" parm:mutableDict completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
       
        
        
        if ([kStr(responseObject[@"code"]) isEqualToString:@"1"]) {
            
            
            self.foods=responseObject[@"meal"];
            
            
            [self.tableView reloadData];
            
        }
        
        
        
    }];

}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 80;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


  EARecommendMealCell * cell = [tableView dequeueReusableCellWithIdentifier:@"EARecommendMealCell" forIndexPath:indexPath];
    
    NSDictionary * d=self.foods[indexPath.row];
    
    
    
    cell.nameLabel.text=d[@"name"];
    

    
    [cell.headImageView setImageWithURL:[NSURL URLWithString:d[@"img"]]];
    
    
    cell.numberLabel.text=@"X7";
    cell.yuyangLabel.text=[NSString stringWithFormat:@"营养元素:%@",d[@"nutrition"]];
    cell.kllLabel.text=[NSString stringWithFormat:@"卡路里:%@",d[@"calory"]];
    cell.num=[NSString stringWithFormat:@"%@",d[@"num"]].intValue;

    
    cell.minusBlock=^(NSString * type){
    
        
        if ([type isEqualToString:@"repair"]) {
            
            self.repairBtn.backgroundColor=kTingColor;
            
            self.repairBtn.enabled=YES;
            
            
        }
        
        
        
        
        if ([type isEqualToString:@"remove"]) {
            [self.foods removeObject:d];
            
            
            [self.tableView reloadData];
        }
        
        
        
        
 
    
    
    };
    
    
    self.repairBtn.hidden=YES;
    
    cell.foodsBtn.hidden=YES;
    
    cell.addBtn.hidden=YES;
    
    
    return cell;


}










-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.foods.count;

}

//
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    UIView * v=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kUI_SCREEN_WIDTH, 150)];
    
    v.backgroundColor=[UIColor whiteColor];
    
    UIView * linView=[[UIView alloc] init];
    
    
    linView.backgroundColor=kLineColors;
    [v addSubview:linView];
    
    [linView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(v).with.offset(2);
        
        make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH-25, 1));
        
        make.left.mas_equalTo(v).with.offset(10);
        
        
        
    }];

    EAMButton * bt1=[[EAMButton alloc] init];
    
    [bt1 setTitle:@"加入餐桌" forState:0];
    [v addSubview:bt1];
    self.addDiningtable=bt1;
    
    bt1.tag=1;
    [bt1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH/2, 30));
    
        make.top.mas_equalTo(v).with.offset(40);
    
        make.left.mas_equalTo(v).with.offset(kUI_SCREEN_WIDTH/4);
        
        
        
        
    }];
    
    
    EAMButton * bt2=[[EAMButton alloc] init];
    bt2.hidden=YES;
    
    [bt2 setTitle:@"推荐补全" forState:0];
    [v addSubview:bt2];
     self.repairBtn=bt2;
    bt2.tag=2;
    
    if (self.foods.count<5) {
        bt2.backgroundColor=kTingColor;
         self.repairBtn.enabled=NO;
    }else{
    
    bt2.backgroundColor=[UIColor lightGrayColor];

    self.repairBtn.enabled=NO;
    }
    
    [bt2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH/3, 30));
        
        make.top.mas_equalTo(v).with.offset(40);
        
        make.right.mas_equalTo(v).with.offset(-30);
        
        
        
        
    }];
    

    
    [self.addDiningtable addTarget:self action:@selector(toMytableORrepear:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.repairBtn addTarget:self action:@selector(toMytableORrepear:) forControlEvents:UIControlEventTouchUpInside];
    
    

    
    
    
    return v;



}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

    return 150;

}

-(void)toMytableORrepear:(UIButton * )bt
{
    
    switch (bt.tag) {
        case 1:
        {
            
            
            
            ApplicationDelegate.isToRoot=YES;
            
            EATabBarController * tvc=(EATabBarController * )self.tabBarController;
            
            
            [tvc.customTabBarView selectItem:1];
            
            tvc.selectedIndex=1;
            
            
            
            
            
            [ApplicationDelegate sendMealWithMeals:self.foods andMealType:self.mealtype andAty_level:self.aty_level];
            
            
            
            
        }
            break;
        case 2:
        {
            
            
            
            
            
        }
            break;
        default:
            break;
    }
    
    
    
}



@end
